import unittest
import os
import ddt
from Page.address_page import *
from common.databaseTool import DatabaseTool
from common.openExcel import *

filePath = os.path.dirname(__file__) + "/testData/测试数据.xlsx"
addressData = Open_excel(filePath, "添加收货地址成功", {"code": str, "phone": str, "tel": str}).read()


@ddt.ddt
class AddressCase(unittest.TestCase):
    def setUp(self):
        self.driver = open_op_browser(path)
        self.ac = AddressPage(self.driver)
        self.ac.open_url(login_url)

    @classmethod
    def setUpClass(cls):
        cls.db = DatabaseTool("168168", "ecshop_db", port=3308)

    def tearDown(self):
        sql = "delete from ecs_user_address where user_id =113"
        self.db.execute_one(sql)
        self.ac.close()

    # 检测添加正确信息  注册成功
    def address(self, country, province, city, district, username, email, address, code, phone, tel):
        # 在页面内点击用户信息进入个人信息修改页面
        self.ac.click_address()
        # 定位选择国家
        self.ac.click_country(country)
        # 定位选择省份
        self.ac.click_province(province)
        # 定位城市
        sleep(5)
        self.ac.click_city(city)
        # 定位区域
        sleep(5)
        self.ac.click_district(district)
        # 定位输入收货人
        self.ac.input_consignee(username)
        # 定位输入邮箱地址
        self.ac.input_addr_email(email)
        # 定位 输入详细地址
        self.ac.input_address(address)
        # 定位邮政编码
        self.ac.input_zip_code(code)
        # 定位电话
        self.ac.input_phone(phone)
        # 定位手机号
        self.ac.input_tel(tel)
        # 定位新增收货地址按钮
        sleep(5)
        self.ac.add_address()
        sleep(5)
        try:
            sql = "select consignee from ecs_user_address where user_id = 113"
            consignee = self.db.fetchOne(sql)["consignee"]
            self.assertEqual(username, consignee, msg="用例执行失败,收货地址未添加成功")
        except Exception as e:
            raise ("用例执行失败")

    @ddt.data(*addressData)
    def test_add_addr_case(self, data):
        self.address(**data)


if __name__ == '__main__':
    unittest.main()
